package com.gcsj.gcsjspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcsj.gcsjspringboot.common.utils.Constant;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.Query;
import com.gcsj.gcsjspringboot.dao.IncentiveDao;
import com.gcsj.gcsjspringboot.entity.Incentive;
import com.gcsj.gcsjspringboot.entity.SysUserEntity;
import com.gcsj.gcsjspringboot.entity.WorkRecordEntity;
import com.gcsj.gcsjspringboot.service.IncentiveService;
import com.gcsj.gcsjspringboot.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IncentiveServiceImpl extends ServiceImpl<IncentiveDao, Incentive> implements IncentiveService {

    @Autowired
    SysUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        String rdDate = (String) params.get("rdDate");
        Long userId = (Long) params.get("userId");

        List<SysUserEntity> users = userService.list(new QueryWrapper<SysUserEntity>()
                .like("name", name));

        List<Long> ids = new ArrayList<>();
        for (SysUserEntity user : users) {
            ids.add(user.getUserId());
        }
        IPage<Incentive> page = null;
        if(userId == 1){
            page = this.page(
                    new Query<Incentive>().getPage(params),
                    new QueryWrapper<Incentive>()
                            .in(StringUtils.isNotBlank(name), "user_id", ids)
                            .eq(StringUtils.isNotBlank(rdDate), "rd_date", rdDate)
            );
        } else {
            page = this.page(
                new Query<Incentive>().getPage(params),
                new QueryWrapper<Incentive>()
                        .in(StringUtils.isNotBlank(name), "user_id", ids)
                        .eq(StringUtils.isNotBlank(rdDate), "rd_date", rdDate)
                        .eq("user_id", userId)
        );

        }

        return new PageUtils(page);
    }
}
